package com.his.struts.action;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.ry.RyDao;
import com.his.struts.dao.ry.RyImp;
import com.his.struts.form.RyForm;
import com.his.struts.util.PageUtil;

/**
 * 荣誉功能类
 * @author liumiaoyue
 *
 */
public class RyAction extends DispatchAction {
	RyDao dao =new RyImp();
	Logger log =Logger.getLogger(RyAction.class);
	/** 
	 * Method getAllRy
	 * 得到所有荣誉
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getAllRy(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String admin = request.getParameter("admin");
		int tiaoshu = 4;
		if("lmy".equals(admin)){
			tiaoshu = 10;
		}
		//判断荣誉id是否为空
		String id=request.getParameter("ryid");
		if(id==null){
			id="";
		}else{
			int ryId =Integer.parseInt(id);
			dao.delRy(ryId);
			request.setAttribute("message", "删除成功");
		}
		
		String sql = "rongyu order by ryId desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, tiaoshu, request);
		if(rs != null){
			// 封装
			List<RyForm> list = dao.getRyBean(rs);
			// 数据放到request中
			request.setAttribute("ryList", list);
		}
		if("lmy".equals(admin)){
			return mapping.findForward("allry");
		}
		return mapping.findForward("ryList");
	}
	
	/** 
	 * Method getAllRy
	 * 更新荣誉
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward updateRy(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//荣誉form
		RyForm ryForm = (RyForm) form;
		//上传图片类型
		String type[]={".jpg",".gif",".png",".jpeg",".bmp"};
		//得到图片名称
		String fileName=ryForm.getPicture().getFileName();
		for (String str: type) {
			//判断图片类型是否匹配
			if(fileName.endsWith(str)){
				//设置名称
				ryForm.setRyPic(fileName);
				//得到真实路径
				String path=request.getRealPath("/images/"+fileName);
				try {
					//图片上传
					InputStream input =ryForm.getPicture().getInputStream();
					OutputStream output =new FileOutputStream(path);
					IOUtils.copy(input, output);
					input.close();
					output.close();
				} catch (FileNotFoundException e) {
					log.error(e.getMessage());
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
			break;
		}
		//进行更新
		dao.updateRy(ryForm);
		
		String sql = "rongyu order by ryId desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<RyForm> list = dao.getRyBean(rs);
			// 数据放到request中
			request.setAttribute("ryList", list);
		}
		request.setAttribute("message", "更新成功");
		return mapping.findForward("allry");
	}
	/** 
	 * Method addRy
	 * 添加荣誉
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward addRy(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//荣誉form
		RyForm ryForm = (RyForm) form;
		//上传图片类型
		String type[]={".jpg",".gif",".png",".jpeg",".bmp"};
		//得到图片名称
		String fileName=ryForm.getPicture().getFileName();
		for (String str: type) {
			//判断图片类型是否匹配
			if(fileName.endsWith(str)){
				//设置名称
				ryForm.setRyPic(fileName);
				//得到真实路径
				String path=request.getRealPath("/images/rongyu/"+fileName);
				try {
					//图片上传
					InputStream input =ryForm.getPicture().getInputStream();
					OutputStream output =new FileOutputStream(path);
					IOUtils.copy(input, output);
					input.close();
					output.close();
				} catch (FileNotFoundException e) {
					log.error(e.getMessage());
				} catch (IOException e) {
					log.error(e.getMessage());
				}
				//添加荣誉
				dao.addRy(ryForm);

				String sql = "rongyu order by ryId desc";
				// 调用分页类 
				ResultSet rs = PageUtil.getPage(sql, 10, request);
				if(rs != null){
					// 封装
					List<RyForm> list = dao.getRyBean(rs);
					// 数据放到request中
					request.setAttribute("ryList", list);
				}
				
			}
			break;
		}
		request.setAttribute("message", "添加成功");
		return mapping.findForward("allry");
	}
	
}